<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    //一般对象
    const person = { sex: 'male', age: 18 }
    person[Symbol.iterator] = () => {
      let index = 0
      return {
        next() {
          index++;
          if (index == 1) {
            return {
              value: person.age,
              done: false
            }
          } else if (index == 2) {
            return {
              value: person.sex,
              done: false
            }
          } else {
            return {
              done: true
            }
          }
        }
      }
    }
    for (let item of person) {
      console.log(item);
    }
    //有索引值和length属性的对象
    const dog = { 0: '公', 1: 'aa', length: 2 }
    dog[Symbol.iterator]=Array.prototype[Symbol.iterator];
    
    // dog[Symbol.iterator] = () => {
    //   let i = 0
    //   return {
    //     next() {
    //       let value, done;
    //       if (i<dog.length) {
    //         value=dog[i]
    //         done=false
    //       }else{
    //         value=undefined;
    //         done=true
    //       }
    //       i++;
    //       return{value,done}
    //     }
    //   }
    // }
    for(let item of dog){
      console.log(item);
    }
  </script>
</body>

</html>